/**
 * @DoubleTLVEncoder.java  COPYRIGHT xiaojian
 * 
 * Created on 2018年9月14日 下午3:00:45
 */
package xj.toolkit.bean.tlv.encode.encoders;

import java.util.Arrays;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import xj.toolkit.bean.tlv.encode.TLVEncodeContext;
import xj.toolkit.bean.tlv.encode.TLVEncoder;

/**
 * @author <a href="mailto:cxj2000@gmail.com">xiaojian.cao</a>
 *
 */
public class DoubleTLVEncoder extends AbstractNumberTLVEncoder implements TLVEncoder {

	@SuppressWarnings("unused")
	private static final Logger logger = LoggerFactory.getLogger(IntTLVEncoder.class);
	private static final int DEFAULT_BYTE_SIZE = 8;

	public List<byte[]> encode(Object from, TLVEncodeContext ctx) {
		byte[] ret = null;

		int byteSize = getAnnotationByteSize(ctx);
		if (-1 == byteSize) {
			byteSize = DEFAULT_BYTE_SIZE;
		}

		if (from instanceof Double) {
			ret = ctx.getNumberCodec().double2Bytes(((Double) from), byteSize);
		} else {
			throw new RuntimeException("IntTLVEncoder: wrong source type. [" + from.getClass() + "]");
		}

		return Arrays.asList(ret);
	}

	public Class<?>[] getFieldType() {
		return new Class<?>[] { double.class, Double.class };
	}

}